package com.hyzs.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {

    static String dateTimeFormat = "yyyy-MM-dd HH:mm:ss";
    static String dateFormat = "yyyy-MM-dd";

    // 中文空格 != \s
    public static String trimBlank(String str){
        if(str != null) {
            return str.replaceAll("[\\s　 ]", "");
        } else return "";
    }

    public static List<String> findBaseLawList(String content){
        String regex = "([《〈（])(中华人民共和国\\S{1,30}?)([）》〉])(第[零一二三四五六七八九十百千]{1,7}?条)";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(trimBlank(content));
        List<String> resList = new ArrayList<>();
        while(matcher.find()){
            resList.add(matcher.group(2)+matcher.group(4));
        }
        return resList;
    }

    public static long getTimestamp(String timeFormat, String dateTime){
        if(dateTime == null || dateTime.equals(""))
            return 0;
        else
            try{
                SimpleDateFormat format = new SimpleDateFormat(timeFormat);
                return format.parse(dateTime).getTime();
            } catch (ParseException e){
                System.err.println(dateTime + " parse time failed!");
                return 0;
            }

    }

    public static Map<String, Object> updateLawMap(Map<String, Object> lawMap) {
        if(lawMap.containsKey("implement_date")){
            String oldDate = lawMap.get("implement_date").toString();
            long newTimestamp = StringUtils.getTimestamp(dateFormat,oldDate);
            lawMap.put("implement_date", newTimestamp);
        }
        if(lawMap.containsKey("publish_date")){
            String oldDate = lawMap.get("publish_date").toString();
            long newTimestamp = StringUtils.getTimestamp(dateFormat,oldDate);
            lawMap.put("publish_date", newTimestamp);
        }
        return lawMap;
    }

    public static Map<String, Object> updateJudgementMap(Map<String, Object> judgeMap) {
        if(judgeMap.containsKey("judgement_date")){
            String oldDate = judgeMap.get("judgement_date").toString();
            long newTimestamp = StringUtils.getTimestamp(dateFormat,oldDate);
            judgeMap.put("judgement_date", newTimestamp);
        }
        if(judgeMap.containsKey("pub_date")){
            String oldDate = judgeMap.get("pub_date").toString();
            long newTimestamp = StringUtils.getTimestamp(dateFormat,oldDate);
            judgeMap.put("pub_date", newTimestamp);
        }
        if(judgeMap.containsKey("create_time")){
            String oldDate = judgeMap.get("create_time").toString();
            long newTimestamp = StringUtils.getTimestamp(dateTimeFormat,oldDate);
            judgeMap.put("create_time", newTimestamp);
        }
        return judgeMap;
    }

    // split content based on regex
    // trim title content
    public static String trimJudgementContent(String rawContent) {
        String regex = "(审判长|审判员|代理审判员)(\\S{1,5}?)((审判员|代理审判员|陪审员|人民陪审员|人民评审员)(\\S{0,5}?)){0,5}(二)";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(rawContent);
        if(matcher.find()){
            int start_idx = matcher.start();
            return rawContent.substring(0, start_idx);
        }
        else
            return rawContent;
    }

}
